//! 数据生成宏实现

/// forge! 宏 - 用于生成结构化数据
#[macro_export]
macro_rules! forge {
    ({ $($key:literal => $val:expr),+ $(,)? }) => {{
        use serde_json::json;
        json!({
            $( $key: $val ),+
        })
    }};
}

/// regex! 宏 - 基于正则表达式生成数据
#[macro_export]
macro_rules! regex {
    ($re:literal) => {{
        use once_cell::sync::Lazy;
        use rand::Rng;
        use rand_regex::Regex;
        use $crate::regions::china::rand_hanzi;

        static CACHED_RE: Lazy<Regex> =
            Lazy::new(|| Regex::compile($re, 100).expect("Invalid regex pattern"));

        let mut rng = rand::thread_rng();
        let mut s: String = CACHED_RE.sample_string(&mut rng, 10);

        // 替换Unicode转义序列为实际汉字
        let re = regex::Regex::new(r"\\[uU][0-9a-fA-F]{4}").unwrap();
        s = re
            .replace_all(&s, |_caps: &regex::Captures| -> String {
                rand_hanzi().to_string()
            })
            .to_string();

        if s.is_empty() {
            let count = rng.gen_range(2..=5);
            s = (0..count).map(|_| rand_hanzi()).collect::<String>() + "市";
        }

        s
    }};
}

/// pattern! 宏 - 支持正则表达式模式生成
#[macro_export]
macro_rules! pattern {
    ($pattern:literal) => {{
        use rand::Rng;
        
        let mut rng = rand::thread_rng();
        
        match $pattern {
            "1[3-9]\\d{9}" => {
                let second_digit = rng.gen_range(3..=9);
                let remaining: String = (0..9).map(|_| rng.gen_range(0..=9).to_string()).collect::<Vec<_>>().join("");
                format!("1{}{}", second_digit, remaining)
            },
            "[a-z]{5,10}@[a-z]{3,8}\\.(com|cn|org)" => {
                let name_len = rng.gen_range(5..=10);
                let domain_len = rng.gen_range(3..=8);
                let name: String = (0..name_len).map(|_| rng.gen_range(b'a'..=b'z') as char).collect();
                let domain: String = (0..domain_len).map(|_| rng.gen_range(b'a'..=b'z') as char).collect();
                let tld = ["com", "cn", "org"][rng.gen_range(0..3)];
                format!("{}@{}.{}", name, domain, tld)
            },
            "\\d{17}[0-9X]" => {
                let first_17: String = (0..17).map(|_| rng.gen_range(0..=9).to_string()).collect::<Vec<_>>().join("");
                let last_char = if rng.gen_bool(0.9) { rng.gen_range(0..=9).to_string() } else { "X".to_string() };
                format!("{}{}", first_17, last_char)
            },
            _ => $pattern.to_string()
        }
    }};
}

/// rand_num! 宏 - 生成随机数字
#[macro_export]
macro_rules! rand_num {
    ($start:expr, $end:expr) => {{
        use rand::Rng;
        let mut rng = rand::thread_rng();
        rng.gen_range($start..=$end)
    }};
}

/// datetime! 宏 - 生成日期时间
#[macro_export]
macro_rules! datetime {
    ("iso") => {{
        use chrono::Utc;
        Utc::now().to_rfc3339()
    }};
    ("timestamp") => {{
        use chrono::Utc;
        Utc::now().timestamp().to_string()
    }};
    ($format:literal) => {{
        use chrono::Utc;
        Utc::now().format($format).to_string()
    }};
}